package manager.server{
	import com.debug.TDebug;
	
	import flash.utils.ByteArray;
	
	import manager.server.event.SocketEvent;
	import manager.server.factory.CommandFactory;
	import manager.server.factory.ISocketCommand;
	
	import org.adobe.utils.Singleton;


	/**
	 * Socket实例 
	 * @author 
	 * 
	 */	
	public class Server {
		private var socket:SocketBase;
		private var _host:String;
		private var _port:int;
		
		private var socket_BackHandler:Function;//接收到socket进行回调处理
		
		private var isReadHead:Boolean = true; 	//是否已经读取了消息头
		private var msgMaxLength:int = 4800; 	//消息的最大长度
		private var msgLength:int; 				//消息的实际长度
		private var headLength:int = 2; 		//消息头长度
		private var byteArray:ByteArray; 		//临时存储byte数组的对象

		public function Server(host:String, port:int){
			this._host = host;
			this._port = port;
			socket = new SocketBase();
			byteArray = new ByteArray();
			
			//返回数据处理完成
			Singleton.getInstance().addEventListener(SocketEvent.READY, readyFunction);
			//套接字接收到数据
			Singleton.getInstance().addEventListener(SocketEvent.RECEIEVED, onReceieveByteArray);
		}

		/**
		 * 启动连接socket 
		 */		
		public function connect():void {
			socket.connect(_host, _port);
		}
		public function get connected():Boolean {
			return socket.connected;
		}
		public function close():void {
			socket.close();
		}
		
		/**
		 * 发送客户端Socket消息
		 * 将字节写入socket
		 * 设置返回函数
		 */
		public function send(byteArray:ByteArray, backFunction:Function):void {
			if (!socket.connected){
				return;
			}
			byteArray.position = 0;
			trace("发送数据长：" + byteArray.length);
			
			socket.writeShort(byteArray.length); //长度
			socket.writeBytes(byteArray);		 //字节内容
			socket.flush();
			
			this.socket_BackHandler = backFunction;
		}

		/**
		 * 接收服务器端的byteArray信息
		 * 用于和C的直接交互
		 */
		private var byte:ByteArray = new ByteArray();
		private function onReceieveByteArray(event:SocketEvent = null):void {
			//TDebug.trace("读取数据长度：" + socket.bytesAvailable); 
			//需要读取消息头
			if (isReadHead){
				if (socket.bytesAvailable >= headLength){ //headLength =2
					msgLength = socket.readShort();       //和发送的格式对应，读取消息的长度
					isReadHead = false;
				}
			}
			if (msgLength <= msgMaxLength && !isReadHead){ //4800
				if (msgLength <= 0){
					return;
				}
				if (socket.bytesAvailable + headLength >= msgLength){
					byteArray.position = 0;
					socket.readBytes(byteArray);
					
					byteArray.position = 0;
					/**工厂处理bytes，派出ready事件*/
					CommandFactory.decodeSocketData(byteArray);
					isReadHead = true;
				}
			}
			//缓冲区中还有可读取的数据
			if (socket.bytesAvailable >= headLength){
				onReceieveByteArray();
			}
		}
		
		/**
		 * 处理指令工厂的回调数据，为回调数据传递参数
		 * @param event
		 */
		private function readyFunction(event:SocketEvent):void {
			//Socket发送类型
			var cmd:int = event.data.cmd;
			
			var socketCommand:ISocketCommand = CommandFactory.getCommandByType(cmd);
			
			//为回调数据传递参数
			socket_BackHandler.call(null, socketCommand);
		}
	}
}